<?php
if (!class_exists('Object')) {
    require LIB.DS.'object.php';
}

class Router extends Object
{     
    function __construct(){   
       
    }
    
    public function connect()
	{
		require_once ROOT .DS.'config' .DS.'router.php';
        return self::get($router);   
	}
    
    public function get($router = false)
    {
        if(!$router) {
            return false;
        }
        // Default controller
        if($this->baseUrl === '') {
            $this->baseUrl = 'home';
        }  
             
        foreach($router as $key => $val) { 
            if(preg_match('#^'.$key.'$#', $this->baseUrl)) {    
                if(strpos($val, '$') !== false && strpos($key, '(') !== false) { 
                    $val = preg_replace('#^'.$key.'$#', $val, $this->baseUrl); 
                }  
                parse_str($val, $this->params);
                if(substr($this->params['controller'], 0, 5) === 'admin') {
                    if(strlen($this->params['controller']) <= 6) {
                        return false;
                    }
                    $this->params['subfix'] = 'admin';
                    $this->params['controller'] = substr($this->params['controller'], 6);
                } 
                else {
                    $this->params['subfix'] = 'web';
                }
                
                // Check action 
                if(!isset($this->params['action'])) {
                    $this->params['action'] = 'index';
                }
                break; 
            } 
        }         
    }
}
?>
